<?php
if (isset($_POST['editer'])) 
{
	$req = $bdd->prepare('SELECT * FROM recettes WHERE id = :id');
	$req->execute(array('id' => $_POST['id']));
	$donnees = $req->fetch();
	?>
	<h1>Editer une recette</h1>
	<form method="post" action="index.php?page=admin_recettes" enctype="multipart/form-data">
		<table border="0" width="100%">
			<tr>
			<td><label for="nom">Nom</label></td>
			<td><input type="text" name="nom" value="<?php echo stripslashes($donnees['nom']); ?>" maxlength="250" size="50" required></td></tr>
			<td><label for="categorie">Categorie</label></td>
			<td><select name="categorie" id="categorie">
			<?php
			$req_cat = $bdd->query('SELECT * FROM categories_recettes');
			while ($donnees_cat = $req_cat->fetch())
			{
				?>
				<option value="<?php echo $donnees_cat['id']; ?>" <?php if ($donnees_cat['id']==$donnees['categorie']) { echo 'selected'; }  ?>>
				<?php echo stripslashes($donnees_cat['nom']); ?>
				</option>
				<?php
			}
			?>
			</select></td></tr>
			<tr><td colspan="2"><input type="file" size="150" name="image" /></td></tr>
			<tr><td colspan="2"><label for="ingredients">Ingrédients</label></td></tr>
			<tr><td colspan="2"><textarea id="ingredients" name="ingredients" rows="10" cols="70"><?php echo $donnees['ingredients']; ?></textarea></td></tr>
			<tr><td colspan="2"><label for="preparation">Préparation</label></td></tr>
			<tr><td colspan="2"><textarea id="preparation" name="preparation" rows="10" cols="70"><?php echo $donnees['preparation']; ?></textarea></td></tr>
			<tr><td colspan="2"><label for="notes">Notes</label></td></tr>
			<tr><td colspan="2"><textarea id="notes" name="notes" rows="10" cols="70"><?php echo $donnees['notes']; ?></textarea></td></tr>
			<tr><td colspan="2"><input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" />
			<input type="submit" value="Mettre à jour" name="modifier" class="bouton" /></td></tr>
		</table>
	</form>
	<?php
}
elseif (isset($_POST['modifier'])) 
{
	$req = $bdd->prepare('UPDATE recettes SET nom=:nom, categorie=:categorie, ingredients=:ingredients, preparation=:preparation, notes=:notes WHERE id=:id');
	$req->execute(array(
		'nom' => stripslashes($_POST['nom']),
		'categorie'  => $_POST['categorie'],
		'ingredients' => stripslashes($_POST['ingredients']),
		'preparation' => stripslashes($_POST['preparation']),
		'notes' => stripslashes($_POST['notes']),
		'id' => $_POST['id']
	));
	if (isset($_FILES['image']) AND $_FILES['image']['error'] == 0)
	{
		if ($_FILES['image']['size'] <= 1000000)
		{
			$infosfichier = pathinfo($_FILES['image']['name']);
			$extension_upload = $infosfichier['extension'];
			$extensions_autorisees = array('jpg', 'jpeg', 'gif', 'png');
			if (in_array($extension_upload, $extensions_autorisees))
			{
				$chemin = 'recettes/'.$_POST['id'].'.'.$extension_upload;
				move_uploaded_file($_FILES['image']['tmp_name'], $chemin);
				
				$req = $bdd->prepare('UPDATE recettes SET image=:image WHERE id=:id');
				$req->execute(array(
					'image' => $chemin,
					'id' => $_POST['id']
				));
			}
		}
	}
	header('Location: index.php?page=admin_recettes'); 
}
elseif (isset($_POST['effacer'])) 
{
	$req = $bdd->prepare('DELETE FROM recettes WHERE id=:id');
	$req->execute(array('id' => $_POST['id']));
	header('Location: index.php?page=admin_recettes'); 
}
elseif (isset($_POST['ajouter'])) 
{
	$req = $bdd->prepare('INSERT INTO recettes (nom, categorie, ingredients, preparation, notes) VALUES (:nom, :categorie, :ingredients, :preparation, :notes)');
	$req->execute(array(
		'nom' => stripslashes($_POST['nom']),
		'categorie'  => $_POST['categorie'],
		'ingredients' => stripslashes($_POST['ingredients']),
		'preparation' => stripslashes($_POST['preparation']),
		'notes' => stripslashes($_POST['notes'])
	));
	header('Location: index.php?page=admin_recettes');
}
elseif (isset($_POST['nouveau'])) 
{
	?>
   <h1>Ajouter une recette</h1>
   <form method="post" action="index.php?page=admin_recettes">
		<table border="0" width="100%">
			<tr>
			<td><label for="nom">Nom</label></td>
			<td><input type="text" name="nom" value="" maxlength="250" size="50" required></td></tr>
			<td><label for="categorie">Categorie</label></td>
			<td><select name="categorie" id="categorie">
			<?php
			$req_cat = $bdd->query('SELECT * FROM categories_recettes ORDER BY nom');
			while ($donnees_cat = $req_cat->fetch())
			{
				?>
				<option value="<?php echo $donnees_cat['id']; ?>">
				<?php echo $donnees_cat['nom']; ?>
				</option>
				<?php
			}
			?>
			</select></td></tr>
			<tr><td colspan="2"><label for="ingredients">Ingrédients</label></td></tr>
			<tr><td colspan="2"><textarea id="ingredients" name="ingredients" rows="10" cols="70"> </textarea></td></tr>
			<tr><td colspan="2"><label for="preparation">Préparation</label></td></tr>
			<tr><td colspan="2"><textarea id="preparation" name="preparation" rows="10" cols="70"> </textarea></td></tr>
			<tr><td colspan="2"><label for="notes">Notes</label></td></tr>
			<tr><td colspan="2"><textarea id="notes" name="notes" rows="10" cols="70"> </textarea></td></tr>
			<tr><td colspan="2"><input type="submit" value="Ajouter cette nouvelle recette" name="ajouter" class="bouton" /></td></tr>
		</table>
	</form>
	<?php
}
else
{
	?>
	<form method="post" action="index.php?page=admin_recettes">
		<input type="submit" value="Ajouter une nouvelle recette" name="nouveau" class="bouton"  />
	</form>
	<?php
	$req_cat = $bdd->query('SELECT * FROM categories_recettes ORDER BY nom');
	while ($donnees_cat = $req_cat->fetch())
	{
		?>
		<h1><?php echo $donnees_cat['nom']; ?></h1>
		<?php
		$req = $bdd->prepare('SELECT * FROM recettes WHERE categorie=:categorie ORDER BY nom');
		$req->execute(array('categorie' => $donnees_cat['id']));
		?>
		<table border="0" width="100%">
			<?php
			while ($donnees = $req->fetch())
			{
				?>
				<form method="post" action="index.php?page=admin_recettes">
					<tr>
					<td><img src="<?php echo $donnees['image']; ?>" width="80" />
					<p><b><?php echo stripslashes($donnees['nom']); ?></b></p>
					<input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" />
					<input type="submit" value="Editer" name="editer" class="bouton" />
					<input type="submit" value="Effacer" name="effacer" class="bouton" />
					</td>
					</tr>
				</form>
				<?php 
			}
			?>
		</table>
		<?php
		$req->closeCursor();
	}
	$req_cat->closeCursor();
}